# $< 第一个输入 $^ 所有输入 $@ 输出
C_SOURCES = $(wildcard kernel/*.c cpu/*.c driver/*.c libc/*.c)
HEADERS = $(wildcard kernel/*.h cpu/*.h driver/*.h libc/*.h)
OBJS = ${C_SOURCES:.c=.o cpu/interrupt.o}

CFLAGS = -g -ffreestanding -Wall -Wextra -fno-exceptions -m32

%.o: %.c ${HEADERS}
	i386-elf-gcc ${CFLAGS} -c $< -o $@

%.o: %.asm
	nasm -felf $< -o $@

kernel.bin: boot/kernel_entry.o ${OBJS}
	i386-elf-ld -o $@ -Ttext 0x1000  $^ --oformat binary

%.bin: %.asm
	nasm -fbin $< -o $@

os-image.bin: boot/bootsect.bin kernel.bin
	cat $^ > $@

os-image-filled.bin: os-image.bin
	dd if=/dev/zero of=$<.tmp bs=1M count=1
	cat $< $<.tmp > $@.tmp
	dd if=$@.tmp of=$@ bs=1M count=1
	rm -f $@.tmp $<.tmp

all: os-image-filled.bin

run: os-image.bin
	qemu-system-x86_64 -fda $<

clean:
	rm -f *.o *.bin *.tmp
	rm -f boot/*.bin boot/*.o kernel/*.o libc/*.o cpu/*.o driver/*.o